#include <iostream>
#include <SFML/Graphics.hpp>
#include "../../include/GUI/ProgressBar.hpp"

using namespace std;

namespace GUI
{
    ProgressBar::ProgressBar(Engine &Moteur, int value, int max_value) : Widget(Moteur)
    {
        if(value > max_value)
            SetValue(max_value);
        else
            SetValue(value);

        m_max_value = max_value;

        m_font = m_Moteur.Config()->GetGUI()->font_contenu;
        m_pourcentage.SetCharacterSize(13);
        m_pourcentage.SetColor(m_Moteur.Config()->GetGUI()->color_contenu);
        m_pourcentage.SetFont(m_font);

        m_sprite.SetImage(*m_Moteur.Graphic()->GetGUIImage(7)); // On charge l'image
        m_rects.push_back(sf::IntRect(174,0,100,30));
        m_rects.push_back(sf::IntRect(0,0,174,37));

        m_dimensions.width = m_rects[1].Width-4*m_padding/3;
        m_dimensions.height = m_sprite.GetSize().y;
    }

    void ProgressBar::SetValue(int value)
    {
        m_value = value;

        // On modifi la variable de texte
        std::ostringstream oss;
        oss << (m_value*100)/m_max_value;
        m_pourcentage.SetString(sf::String(oss.str()+"%"));
    }
    int ProgressBar::GetValue()
    {
        return m_value;
    }

    void ProgressBar::SetMaxValue(int max_value)
    {
        m_max_value = max_value;

        // On modifi la variable de texte
        std::ostringstream oss;
        oss << (m_value*100)/m_max_value;
        m_pourcentage.SetString(sf::String(oss.str()+"%"));
    }
    int ProgressBar::GetMaxValue()
    {
        return m_max_value;
    }

    bool ProgressBar::IsMax()
    {
        if(m_value == m_max_value)
            return true;
        else
            return false;
    }
    bool ProgressBar::IsMin()
    {
        if(m_value == 0)
            return true;
        else
            return false;
    }
    sf::Text& ProgressBar::GetText()
    {
        return m_pourcentage;
    }

    void ProgressBar::Afficher()
    {
        if(m_value > 0)
        {
            m_sprite.SetPosition(m_position.x+20, m_position.y+6);
            m_sprite.SetSubRect(m_rects[0]);
            m_sprite.SetScaleX((float)(m_value)/(float)(m_max_value));
            m_Moteur.Graphic()->Draw(m_sprite);
        }

        m_sprite.SetPosition(m_position.x, m_position.y);
        m_sprite.SetSubRect(m_rects[1]);
        m_sprite.SetScaleX(1);
        m_Moteur.Graphic()->Draw(m_sprite);

        m_pourcentage.SetPosition(m_position.x+133, m_position.y+m_padding);

        m_Moteur.Graphic()->Draw(m_pourcentage);
    }
    void ProgressBar::UpdateEvent()
    {

    }
}
